package com.ruoyi.resource.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.ruoyi.oss.config.OssConfig;

import org.springframework.stereotype.Service;
import com.ruoyi.resource.mapper.AcademicResourcesMapper;
import com.ruoyi.resource.domain.AcademicResources;
import com.ruoyi.resource.service.IAcademicResourcesService;
import com.basic.service.impl.BaseServiceImpl;

/**
 * 学术资源Service业务层处理
 * 
 * @author 王强
 * @date 2024-01-21
 */
@Service
public class AcademicResourcesServiceImpl extends BaseServiceImpl<Long, AcademicResources> implements IAcademicResourcesService 
{
	OssConfig config;
	
	@Resource
    public void setConfig(OssConfig config) {
		this.config = config;
	}
	
    public AcademicResourcesServiceImpl(AcademicResourcesMapper baseMapper) {
		super(baseMapper);
		// TODO Auto-generated constructor stub
	}
    /**
     * 查询学术资源
     * 
     * @param id 学术资源主键
     * @return 学术资源
     */
    @Override
    public AcademicResources selectById(Integer id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询学术资源列表
     * 
     * @param academicResources 学术资源
     * @return 学术资源
     */
    @Override
    public List<AcademicResources> selectList(AcademicResources academicResources)
    {
    	List<AcademicResources> selectList = baseMapper.selectList(academicResources);
         
         if (0 == selectList.size()) {
         	return selectList;
         }
         
         String prefix = "http://".concat(config.getURL());
         
         for (AcademicResources item : selectList) {
 			
        	 item.setCover(prefix.concat(item.getCover()));
 		 }
         
 		return selectList;
    }

    /**
     * 新增学术资源
     * 
     * @param academicResources 学术资源
     * @return 结果
     */
    @Override
    public int insert(AcademicResources academicResources)
    {
    	insertProperties(academicResources);
    	common(academicResources);
        return baseMapper.insert(academicResources);
    }

    /**
     * 修改学术资源
     * 
     * @param academicResources 学术资源
     * @return 结果
     */
    @Override
    public int update(AcademicResources academicResources)
    {
    	updateProperties(academicResources);
    	
    	common(academicResources);
    	
        return baseMapper.update(academicResources);
    }
    

    private void common(AcademicResources news) {
    	
    	String pic = news.getCover();
    	
    	if (null == pic) {
    		return;
    	}
    	
    	String prefix = (config.getURL());

		

		pic = pic.replaceAll("http://", "").replaceAll("https://", "").replaceAll(prefix, "");

		news.setCover(pic);
    }

    

    /**
     * 批量删除学术资源
     * 
     * @param ids 需要删除的学术资源主键
     * @return 结果
     */
    @Override
    public int deleteByIds(Integer[] ids)
    {
        return baseMapper.deleteByIds(ids);
    }

    /**
     * 删除学术资源信息
     * 
     * @param id 学术资源主键
     * @return 结果
     */
    @Override
    public int deleteById(Integer id)
    {
        return baseMapper.deleteById(id);
    }
}
